package com.wangsd.web.sys.service;

import com.gitee.fastmybatis.core.query.Query;
import com.wangsd.common.enums.ButtonType;
import com.wangsd.web.sys.entity.SysButton;
import com.wangsd.web.sys.entity.SysMenu;
import com.wangsd.web.sys.entity.SysPermission;
import com.wangsd.web.sys.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysMenuService {
    @Resource
    SysMenuMapper mapper;
    @Autowired
    SysPermissionService permissionService;
    @Autowired
    SysButtonService buttonService;

    /**
     * 查询超级管理员拥有的所有菜单
     *
     * @return
     */
    public List<SysMenu> selectByAdmin() {
        List<SysMenu> list = mapper.list(new Query());
        List<SysButton> buttonList = buttonService.selectList();
        Map<Long, List<SysButton>> buttonMap = buttonList.stream().collect(Collectors.groupingBy(SysButton::getMenuId));
        for (SysMenu item : list) {
            List<SysButton> sysButtonList = buttonMap.get(item.getId());
            if (sysButtonList != null) {
                List<String> actions = sysButtonList.stream().map(SysButton::getAction).collect(Collectors.toList());
                item.setActions(actions);
            }
            item.setButtonList(sysButtonList);
        }
        return list;
    }

    /**
     * 根据角色id查询拥有的菜单
     *
     * @param roleId
     * @return
     */
    public List<SysMenu> selectByRoleId(Long roleId) {
        List<SysMenu> list = mapper.selectByRoleId(roleId);
        List<SysPermission> sysPermissions = permissionService.selectByRoleId(roleId);

        Map<Long, List<SysPermission>> permissionMap = null;
        if (sysPermissions != null) {
            permissionMap = sysPermissions.stream().collect(Collectors.groupingBy(SysPermission::getMenuId));
        }

        if (list != null) {
            for (SysMenu sysMenu : list) {
                if (permissionMap != null) {
                    List<SysPermission> permissionVoList = permissionMap.get(sysMenu.getId());
                    if (permissionVoList != null) {
                        List<String> actions = permissionVoList.stream().map(SysPermission::getAccessSign).collect(Collectors.toList());
                        sysMenu.setActions(actions);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 查询所有一级菜单
     *
     * @return
     */
    public List<SysMenu> selectParentMenu() {
        Query query = new Query();
        query.isNull("pid");
        List<SysMenu> list = mapper.list(query);
        return list;
    }

    /**
     * 查询所有二级菜单
     *
     * @return
     */
    private List<SysMenu> selectChildMenu() {
        Query query = new Query();
        query.notNull("pid");
        List<SysMenu> list = mapper.list(query);
        return list;
    }

    /**
     * 查询所有菜单
     * @return
     */
    public List<SysMenu> selectAll() {
        List<SysMenu> parntList = selectParentMenu();

        List<SysMenu> childList = selectChildMenu();
        Map<Long, List<SysMenu>> childMap = childList.stream().collect(Collectors.groupingBy(SysMenu::getPid));
        List<SysButton> sysbuttonList = buttonService.selectList();
        Map<Long, List<SysButton>> buttonMap = sysbuttonList.stream().collect(Collectors.groupingBy(SysButton::getMenuId));

        for (SysMenu sysMenu : parntList) {
            /*取出父菜单下的所有子菜单*/
            List<SysMenu> child = childMap.get(sysMenu.getId());
            List<SysMenu> childMenuList = new ArrayList<>();
            if (child == null) {
                continue;
            }
            for (SysMenu menu : child) {
                /*取出子菜单所对应的按钮*/
                List<SysButton> buttonList = buttonMap.get(menu.getId());
                //放按钮权限集
                if (buttonList != null) {
                    List<String> actions = buttonList.stream().map(SysButton::getAction).collect(Collectors.toList());
                    menu.setActions(actions);
                    menu.setButtonList(buttonList);
                } else {
                    menu.setActions(new ArrayList<>());
                    menu.setButtonList(new ArrayList<>());
                }
                childMenuList.add(menu);
            }
            sysMenu.setChildren(childMenuList);
        }
        return parntList;
    }

    /**
     * 删除菜单(同时删除按钮)
     * @param menuId
     * @return
     */
    @Transactional
    public int delete(Long menuId) {
        buttonService.deleteByMenuId(menuId);
        mapper.deleteByQuery(new Query().eq("pid", menuId));
        return mapper.deleteById(menuId);
    }

    @Transactional
    public void saveOrUpdate(SysMenu entity) {
        if (entity.getId() != null) { //修改
            buttonService.deleteByMenuId(entity.getId());
            mapper.updateIgnoreNull(entity);
        } else { //新增
            mapper.saveIgnoreNull(entity);
        }


        //新增菜单对应的按钮
        if (entity.getActions() != null) {
            List<SysButton> addButton = new ArrayList<>();
            for (String item : entity.getActions()) {
                SysButton button = new SysButton();
                button.setMenuId(entity.getId());
                button.setAction(item);
                button.setName(ButtonType.getValueByCode(item));
                addButton.add(button);
            }
            buttonService.saveBatch(addButton);
        }

    }
}
